package job.wangyi;

import java.util.Scanner;

/**
 * @author wq
 * 给定一个N层的完全二叉树，根据从上到下，从左往右的顺序，对每个节点进行编号。
 * 接下来再指定一个节点，将该节点的两颗子树进行翻转
 * i的左子树为 2*i   右子树为2*i+1
 *
 * 测试用例
 * 4 3
 * 1 2 3 4 5 7 6 8 9 10 11 14 15 12 13
 *
 * 4 8
 * 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
 */
public class WangQuanErChaShuFanZhuan {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();

        int n = (int) Math.pow(2, N) - 1;//节点的个数
        int[] treeNodes = new int[n];//初始化一个数组
        // System.out.println(n);
        for (int i = 0; i < n; i++) {
            treeNodes[i] = i + 1;
        }



        WangQuanErChaShuFanZhuan solution = new WangQuanErChaShuFanZhuan();

        int index = M - 1;//指定节点的下标
        int left = index * 2 + 1;//左孩子
        int right = index * 2 + 2;//右孩子
        solution.reverce(treeNodes, left, right, n);//递归进行翻转

        for (int e : treeNodes) {
            System.out.print(e + " ");
        }
    }

    public void reverce(int[] treeNodes, int left, int right, int len) {
        if (left < 0 || left >= len || right < 0 || right >= len) {
            return;
        }

        /*
         * left === right
         *
         * left.left === right.left
         * 2*left+1<====>2*right+1
         *
         * left.right===right.right
         * 2*left+2<====>2*right+2
         * */
        swap(treeNodes, left, right);
        reverce(treeNodes, 2 * left + 1, 2 * right + 1, len);
        reverce(treeNodes, 2 * left + 2, 2 * right + 2, len);



    }

    public void swap(int[] nums, int i, int j) {
        int len = nums.length;
        if (i < 0 || i >= len || j < 0 || j >= len) {
            return;
        }
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

}
